package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


@RestController
public class WebController {

    @Resource
    private UserService userService;
    @Resource
    private AdminService adminService;

    @GetMapping("/hello")
    public String hello() {
        return "Hello";
    }

    /**
     * 管理员/员工登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account account) {
        Account result = null;
        if ("ADMIN".equals(account.getRole())) {  // 管理员登录（保持不变，假设ADMIN在ENUM中）
            result = adminService.login(account);
        } else if ("USER".equals(account.getRole())) {  // 员工角色改为数据库ENUM中的USER
            result = userService.login(account);
        } else {
            throw new CustomException("500","非法输入");
        }
        return Result.success(result);
    }

    /**
     * 员工注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        userService.register(user);
        return Result.success();
    }

    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody Account account) {
        if ("ADMIN".equals(account.getRole())) {  // 管理员角色（保持与数据库 ENUM 一致）
            adminService.updatePassword(account);
        } else if ("USER".equals(account.getRole())) {  
            userService.updatePassword(account);
        } else {
            throw new CustomException("500","非法输入");
        }
        return Result.success();
    }

   /*
    @GetMapping("/barData")
    //获取部门人员分布的柱状图数据
    public Result getBarData() {
        Map<String, Object> map = new HashMap<>();
        List<User> userList = userService.selectAll(null);
        Set<String> departmentNameSet = userList.stream().map(User::getDepartmentName).collect(Collectors.toSet());
        map.put("department", departmentNameSet);  // x轴数据
        List<Long> countList = new ArrayList<>();
        for (String departmentName : departmentNameSet) {
            // 统计这个部门下面的员工的数量
            long count = userList.stream().filter(user -> user.getDepartmentName().equals(departmentName)).count();
            countList.add(count);
        }
        map.put("count", countList);  // y轴员工数量的数据
        return Result.success(map);
    }
//获取时间趋势的折线图数据（基于文章发布数量）
    @GetMapping("/lineData")
    public Result getLineData() {
        Map<String, Object> map = new HashMap<>();

        Date date = new Date();  // 今天当前的时间
        DateTime start = DateUtil.offsetDay(date, -7); // 起始日期
        List<DateTime> dateTimeList = DateUtil.rangeToList(start, date, DateField.DAY_OF_YEAR);
        // 把 DateTime 类型的日期转换成  字符串类型的日期  ["10月22日", "10月23日"...]
        List<String> dateStrList = dateTimeList.stream().map(dateTime -> DateUtil.format(dateTime, "MM月dd日"))
                .sorted(Comparator.naturalOrder()).collect(Collectors.toList());
        map.put("date", dateStrList);  // x轴数据

        List<Integer> countList = new ArrayList<>();
        for (DateTime day : dateTimeList) {
            // 10月22日
            String dayFormat = DateUtil.formatDate(day);  // 2023-10-22

            Integer count = articleService.selectCountByDate(dayFormat);
            countList.add(count);
        }
        map.put("count", countList);  // y轴发布文章的数量数据
        return Result.success(map);
    }
//获取部门人员分布的饼图数据
    @GetMapping("/pieData")
    public Result getPieData() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<User> userList = userService.selectAll(null);
        Set<String> departmentNameSet = userList.stream().map(User::getDepartmentName).collect(Collectors.toSet());
        for (String departmentName : departmentNameSet) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", departmentName);
            // 统计这个部门下面的员工的数量
            long count = userList.stream().filter(user -> user.getDepartmentName().equals(departmentName)).count();
            map.put("value", count);
            list.add(map);
        }
        return Result.success(list);
    }*/

}
